import { _decorator, Button, Color, Component, instantiate, Label, Node, Prefab, Sprite } from 'cc';
import { LayerBase } from './LayerBase';
import { UserInfo } from './UserInfo';
import { UILayerMgr } from './UILayerMgr';
import { EquipCellItem } from './EquipCellItem';
import { StartSceneGameMgr } from './StartSceneGameMgr';
import { EquipPanel } from './EquipPanel';
import { keepOneDecimalFullFomatFloat } from '../utils/utils';
import { qualityEnum, roleEquipClassEnum } from '../Enum';
import { AudioMgr } from '../utils/AudioMgr';
const { ccclass, property } = _decorator;

@ccclass('DesEquipPanel')
export class DesEquipPanel extends LayerBase {
    
    @property(Label)
    nameLabel:Label;
    @property(Label)
    attackLabel:Label;
    @property(Label)
    frequencyLabel:Label;
    @property(Label)
    criticalHitLabel:Label;
    @property(Label)
    desLabel:Label;
    @property(Node)
    equipRoot:Node;
    @property(Prefab)
    equipCellItemPre:Prefab=null;
    @property(Button)
    putOnOffBtn:Button;
    @property(Label)
    baseAttackLabel:Label;
    @property(Label)
    enhanceAttackBaseLabel:Label;
    @property(Node)
    attackEnhanceDes:Node;
    @property(Label)
    classLabel:Label
    @property(Label)
    qualityClassLabel:Label

    equip;

    init(somedData: any=null): void {
        this.equip=somedData;

        let qualityStr = "";
        switch (somedData.equipTp.quality) {
            case qualityEnum.putong:
                qualityStr="普通";
                break;
            case qualityEnum.xiyou:
                qualityStr="稀有";
                break;
            case qualityEnum.shishi:
                qualityStr="史诗";
                break;
            case qualityEnum.chuanshuo:
                qualityStr="传说";
                break;
        }

        this.nameLabel.string = somedData.equipTp.name;
        this.desLabel.string = "简介:"+somedData.equipTp.des;
        this.qualityClassLabel.string="品质:"+qualityStr;
        this.attackLabel.string = `${somedData.attackAddition}`;
        this.frequencyLabel.string = `${keepOneDecimalFullFomatFloat(somedData.frequencyAddition,2)}（每秒发射一次）`;
        this.criticalHitLabel.string = `+${keepOneDecimalFullFomatFloat(somedData.criticalHitAddition*100,1)}%`;
        this.baseAttackLabel.string="基础:"+this.equip.equipTp.attack;
        this.enhanceAttackBaseLabel.string="强化:"+(somedData.attackAddition-this.equip.equipTp.attack);
        
        let pre=instantiate(this.equipCellItemPre);
        pre.setParent(this.equipRoot);
        pre.getComponent(EquipCellItem).init(somedData);

        if (UserInfo.hasRoleDic.containsKey(StartSceneGameMgr.ins.roleSelectId)) {
            this.putOnOffBtn.enabled=true;
            this.putOnOffBtn.node.getComponent(Sprite).color=Color.WHITE;

            if (somedData.ownerRoleId!=-1) {
                this.putOnOffBtn.node.getChildByName("Label").getComponent(Label).string = "卸下"
            }
            else {
                let result=UserInfo.hasRoleDic.get(StartSceneGameMgr.ins.roleSelectId).getSelfEquipByEquipClass(this.equip.equipTp.equipClass);
                if (result) {
                    this.putOnOffBtn.node.getChildByName("Label").getComponent(Label).string = "替换"
                }
                else {
                    this.putOnOffBtn.node.getChildByName("Label").getComponent(Label).string = "穿戴"
                }
            }
        }
        else{
            this.putOnOffBtn.node.getChildByName("Label").getComponent(Label).string = "封锁";
            this.putOnOffBtn.enabled=false;
            this.putOnOffBtn.node.getComponent(Sprite).color=new Color().fromHEX("#6D6D6D");
        }
      

        if (somedData.enhanceLevel==0) {
            this.attackEnhanceDes.active=false
        }
        else{
            this.attackEnhanceDes.active = true
        }

        let classStr=""
        switch (this.equip.equipTp.equipClass) {
            case roleEquipClassEnum.maozi:
                classStr="帽子"
                break;
            case roleEquipClassEnum.jiezhi:
                classStr="戒指"
                break;
            case roleEquipClassEnum.xiezi:
                classStr="鞋子"
                break;
        }
        this.classLabel.string="类型："+classStr;

    }

    onclickPutOnOffBtn(){
        AudioMgr.ins.playOneShot("audio/clickBtn",0.8);

        if ( this.putOnOffBtn.node.getChildByName("Label").getComponent(Label).string=="卸下") {
            let thisEquip=UserInfo.hasEquipDic.get(this.equip.equipId);
            thisEquip.ownerRoleId=-1
        }
        else if( this.putOnOffBtn.node.getChildByName("Label").getComponent(Label).string=="穿戴"){
            UserInfo.hasEquipDic.get(this.equip.equipId).ownerRoleId=StartSceneGameMgr.ins.roleSelectId;
        }
        else if ( this.putOnOffBtn.node.getChildByName("Label").getComponent(Label).string=="替换") {
            UserInfo.hasRoleDic.get(StartSceneGameMgr.ins.roleSelectId).getSelfEquipByEquipClass(this.equip.equipTp.equipClass).ownerRoleId=-1;
            UserInfo.hasEquipDic.get(this.equip.equipId).ownerRoleId=StartSceneGameMgr.ins.roleSelectId;
        }

        StartSceneGameMgr.ins.saveGameDate();
        UILayerMgr.getIns.resfreshUI("equipPanel")
        UILayerMgr.getIns.closeUILayer(this.node.name)
    }

    onclickOpenEnhancePanelBtn() {
        AudioMgr.ins.playOneShot("audio/clickBtn",0.8);

        UILayerMgr.getIns.openUILayer("enhanceEquipPanel",this.equip);
    }

    onclickOpenSalvagePanelBtn() {
        AudioMgr.ins.playOneShot("audio/clickSalvageBtn",0.8);

        UILayerMgr.getIns.openUILayer("salvageEquipPanel",this.equip);
    }

}


